
package com.tianyang.yukuaifu.common.view;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BasePopWindow;

/**
 * Description : 底部弹窗
 */
public class SelectPopWindow extends BasePopWindow {
    private PopupWindow pw;

    private TextView confirm, titleTv;

    private static boolean isLock;

    public SelectPopWindow(Activity context, View parentView, View addView) {
        super(context);
        SelectPopWindow(context, parentView, addView, null);
    }

    public SelectPopWindow(Activity context, View parentView, View addView, String title) {
        super(context);
        SelectPopWindow(context, parentView, addView, title);
    }

    public void SelectPopWindow(Activity context, View parentView, View addView, String title) {
        if (isLock) {
            return;
        }
        isLock = true;
        pw = this;
        LayoutInflater inflater = (LayoutInflater)context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View pwView = inflater.inflate(R.layout.view_select_pop, null);
        RelativeLayout rlOut = (RelativeLayout)pwView.findViewById(R.id.rl_main_app_subset_out);

        TextView cancle = (TextView)pwView.findViewById(R.id.show_recore_cancle);
        confirm = (TextView)pwView.findViewById(R.id.show_recore_confirm);
        titleTv = (TextView)pwView.findViewById(R.id.titleTv);
        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);
        } else {
            titleTv.setVisibility(View.GONE);
        }

        LinearLayout llCenter = (LinearLayout)pwView.findViewById(R.id.ll_center);
        llCenter.addView(addView);

//        if (ScreenUtil.checkDeviceHasNavigationBar()) {
//            ToastUtil.showTextViewPrompt("有底部功能栏");
//            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)llCenter
//                    .getLayoutParams();
//            params.setMargins(0, 0, 0, ScreenUtil.getNavigationBarHeight());
//            llCenter.setLayoutParams(params);
//        } else {
//            ToastUtil.showTextViewPrompt("没有底部功能栏");
//        }

        // 设置SelectPicPopupWindow的View
        pw.setContentView(pwView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        pw.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        pw.setHeight(ActionBar.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        pw.setFocusable(true);
        // 设置之后点击返回键关闭popuwindow
        pw.setBackgroundDrawable(new ColorDrawable(0x00000000));
        pw.setOutsideTouchable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        pw.setAnimationStyle(R.style.PopupAnimation);
        pw.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        pw.showAtLocation(parentView, Gravity.CENTER, 0, 0);
        darkenBackGround(0.4f);
        rlOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pw.dismiss();
            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pw.dismiss();
            }
        });
        setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                isLock = false;
            }
        });
    }

    public void setConfirmListener(View.OnClickListener listener) {
        if (confirm != null) {
            confirm.setOnClickListener(listener);
        }
    }

}
